// 该mixin目前只用于事件页面表单设计器预览多语言翻译

import { useLocaleInject } from '@hooks/useLocale'
import { useFormMultiLanguage } from '@hooks/useFormMultiLanguage'
import { useI18nExtend } from 'pub-bbx-global/lang/dist'
import i18n from '@src/locales'

export default {
  inject: {
    // _source 页面来源
    _source: {
      default: null
    } 
  },
  setup(){
    const { locale: formPreviewLocale } = useLocaleInject('formPreviewLocale');
    const { isOpenMultiLanguage } = useFormMultiLanguage();

    return {
      formPreviewLocale,
      isOpenMultiLanguage,
    };
  },
  data(){
    return {
      formI18n: useI18nExtend(i18n, () => this.formPreviewLocale)
    }
  },
  computed:{
    isMultiSource() {
      // 国际化全面开放
      return true
      return ['event', 'product'].includes(this._source);
    },
    _displayName(){
      if(this.isOpenMultiLanguage && this.field?.displayNameLanguage){
        // 老数据显示默认中文
        return this.field.displayNameLanguage[this.formPreviewLocale] || this.field.displayName
      }
      return this.field.displayName
    },
    _placeHolder(){
      if(this.isOpenMultiLanguage && this.field?.placeHolderLanguage){
        // 老数据显示默认中文
        return this.field.placeHolderLanguage[this.formPreviewLocale] || this.field.placeHolder
      }
      return this.field.placeHolder
    },
    _placeHolder2(){
      if(this.isOpenMultiLanguage && this.field?.placeHolderLanguage){
        // 老数据显示默认中文
        return this.field.placeHolderLanguage[this.formPreviewLocale] || this.field.defaultTips || this.field.placeHolder
      }
      return this.field.defaultTips || this.field.placeHolder
    },
    _relationField(){
      return this.field?.setting.relationFieldLanguage
    }
  },
  methods:{
    returnDisplayName(field){
      try {
        let { placeHolderLanguage, displayName, displayNameLanguage } = field;
        return displayNameLanguage[this.formPreviewLocale] || displayName;
      } catch (error) {
        console.warn(error, 'error try catch');
        return field.displayName;
      }
    }
  }
}